package view 
{
	import control.command.StartupCommand;
	import control.manager.GSaveManager;
	import control.manager.MapManager;
	import control.manager.RoleManager;
	import control.util.GameFacade;
	
	import data.GameConst;
	import data.PlayerData;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.system.Security;
	
	import view.layer.MapLayer;
	import view.stage.StageBase;
	import view.stage.StageGame;
	import view.stage.StageLoading;
	import view.ui.UILogin;
	
	

	/**
	 * 主程序 
	 * @author leo
	 * 
	 */	
	
	[SWF(width="800",height="600",frameRate="24",backgroundColor="0")]
	public class Immortal extends Sprite
	{
		public static var instance :Immortal;
		public var curStage :*;
		
		private var loginUI :UILogin;
		
		/**
		 * 小loading界面 
		 */		
		public var smallLoading :MovieClip;
		
		public var versions :String = "leo_1.0";
		public function Immortal()
		{
			Security.allowDomain("*");
			GameFacade.reg(StartupCommand);
			initGame();
			instance = this;
			
		}
		
		public function setStage(newView :*):void
		{
			addChild(newView)
			if(this.curStage)
			{
				this.curStage.destroy();
				if(this.curStage.parent)
				{
					this.curStage.parent.removeChild(this.curStage);
				}
			}
			this.curStage = newView;
		}
		
		public function getVisibleWH():Point
		{
			var result :Point;
			if(stage)
			{
				result = new Point(stage.stageWidth,stage.stageHeight);
			}else
			{
				trace("获取舞台宽高失败，stage为null");
			}
			
			return result;
		}
		
		
		private function initGame():void
		{
			if(stage)
			{
				GameFacade.send(StartupCommand.NAME,this);
				//小loading
				this.smallLoading = new MC_InitLoading();
				this.smallLoading.x = stage.stageWidth/2;
				this.smallLoading.y = stage.stageHeight/2;
				this.addChild(this.smallLoading);
				
				var obj :PlayerData = GSaveManager.instance.getPlayerData();
				if(obj)   //如果有用户数据，则跳过创建角色,直接开始load资源
				{
					var stageLoading :StageLoading = new StageLoading();
					stageLoading.clickStart = true;
					this.curStage = stageLoading;
					this.addChild(this.curStage);
					RoleManager.instance.playerData = obj;
				}else      //否则显示创建角色
				{
					this.loginUI = new UILogin();
					addChild(this.loginUI);
				}
			}else
			{
				addEventListener(Event.ADDED_TO_STAGE,initGame);
			}
		}
		
		public function get gameStage():StageGame
		{
			if(this.curStage)
			{
				if(this.curStage is StageGame)
				{
					return this.curStage as StageGame;
				}
			}
			
			return null;
		}
		
	}
}